<?php

namespace Home\Controller;

use Think\Controller;

class IndexController extends BaseController {
	/**
	 * http://127.0.0.1/jinzunlobby/ThinkPHP/index.php/Home/Index/mb_RecoverUserInfo
	 */
	public function mb_RecoverUserInfo() {
		$lUserInfo = M ( 'Userinfo', '', 'DB_LOBBY' );
		$mUserInfo = M ( 'Userinfo' );
		
		$mUserData = $mUserInfo->select ();
		for($i = 0; $i < count ( $mUserData ); $i ++) {
			$id = $mUserData [$i] ['id'];
			$name = $mUserData [$i] ['name'];
			$basePass = $mUserData [$i] ['basepass'];
			$encPass = md5 ( $basePass );
			$nick = $mUserData [$i] ['nick'];
			$coin = $mUserData [$i] ['coin'];
			$phone = $mUserData [$i] ['phone'];
			$openId = $mUserData [$i] ['openid'];
			$unionId = $mUserData [$i] ['unionid'];
			$head = $mUserData [$i] ['head'];
			$regist = $mUserData [$i] ['regist'];
			$code = $mUserData [$i] ['code'];
			
			$lUserInfo->add ( array (
					'name' => $name,
					'basepass' => $basePass,
					'encpass' => $encPass,
					'nick' => $nick,
					'coin' => $coin,
					'phone' => $phone,
					'openid' => $openId,
					'unionid' => $unionId,
					'head' => $head,
					'registdate' => $regist,
					'code' => $code,
					'status' => 0 
			) );
		}
		echo "更新玩家信息完成";
	}
	/**
	 */
	public function mb_TestConnect() {
		// $data = $this->mb_OnMessage (false);
		
		// print_r($data);
		
		// $this->mb_OutMessage ( ErrCode_Normal, json_encode ( array (
		// 'name' => $data->name,
		// 'pass' => $data->pass
		// ) ) );
		$mpzzReq = new \Req_sfsMPZZ ();
		// print_r($mpzzReq->GetConfig());
		// $reqData = json_decode($mpzzReq->GetConfig());
		//
		// $reqData = $this->mb_DecodeSFSData ( $mpzzReq->SetConfig('0', '明牌', '100', '1000', '1') );
		// $reqData = $this->mb_DecodeSFSData ( $mpzzReq->GetConfig() );
		// $reqData = $this->mb_DecodeSFSData ( $mpzzReq->GetTotalAccount() );
		// $reqData = $this->mb_DecodeSFSData ( $mpzzReq->GetDetailAccount());
		// $reqData = $this->mb_DecodeSFSData ( $mpzzReq->GetDetailAccount());
		// $reqData = $this->mb_DecodeSFSData ( $mpzzReq->GetBetDetail ( '0', '100028' ) );
		// $reqData = $this->mb_DecodeSFSData ( $mpzzReq->GetDaMaAccount () );
		// $reqData = $this->mb_DecodeSFSData ( $mpzzReq->GetPaiLu () );
		$reqData = $this->mb_DecodeSFSData ( $mpzzReq->GetRealTimeSeatData () );
		// $reqData = $this->mb_DecodeSFSData ( $mpzzReq->SubmitCMD('3') );
		// $reqData = $this->mb_DecodeSFSData ( $mpzzReq->GetNetworkStatus() );
		print_r ( $reqData->data );
		echo "<br>";
		// print_r ( $reqData->data->JS);//SZW
		// echo "<br>";
		// print_r($reqData->data[0]);
		// if($reqData->state == true){
		// echo "12321";
		// }else {
		// echo "35423";
		
		// }
	}
	/**
	 * http://127.0.0.1/jinzunlobby/thinkphp/index.php
	 */
	public function index() {
		$userInfo = M ( 'Userinfo' );
		$userData = $userInfo->select ();
		for($i = 0; $i < count ( $userData ); $i ++) {
			if ($userData [$i] ['nick'] == '') {
				$id = $userData [$i] ['id'];
				$name = $userData [$i] ['name'];
				$userInfo->where ( "id='$id'" )->save ( array (
						'nick' => $name 
				) );
			}
		}
		echo "更新玩家信息完成";
		// $platform = 1;
		// if (strpos ( $_SERVER ['HTTP_USER_AGENT'], 'iPhone' ) || strpos ( $_SERVER ['HTTP_USER_AGENT'], 'iPad' )) {
		// $platform = 2;
		// } else if (strpos ( $_SERVER ['HTTP_USER_AGENT'], 'Android' )) {
		// $platform = 1;
		// }
		
		// $lobbyInfo = M ( 'Lobbyinfo' );
		// $lobbyData = $lobbyInfo->where ( "id='$platform'" )->find ();
		// $webUrl = $lobbyData ['web'];
		// $date = $lobbyData ['updatedate'];
		// $ver = $lobbyData ['ver'];
		
		// $this->assign ( array (
		// 'projectname' => C ( 'PROJECT_NAME' ),
		// 'weburl' => $webUrl,
		// 'date' => $date,
		// 'apkurl' => C ( 'APK_URL' ),
		// 'ipaurl' => C ( 'IPA_URL' ),
		// 'version' => $ver
		// ) );
		// $this->display ();
		// $appInfo = M ( 'Appinfo' );
		// echo $appInfo->count ();
	}
	/**
	 * 创建一级账号
	 * http://127.0.0.1/jinzunlobby/ThinkPHP/index.php/Home/Index/mb_CreateAdmin
	 */
	public function mb_CreateAdmin() {
		$name = C ( 'ADMIN_NAME' );
		
		$adminInfo = M ( 'Admininfo' );
		$adminData = $adminInfo->where ( "name='$'" )->find ();
		
		if (isset ( $adminData )) {
			echo "一级账号已创建！无需重复创建";
			return;
		}
		
		$nick = '一级账号';
		$basePass = C ( 'ADMIN_PASS' );
		$encPass = md5 ( $basePass . C ( 'MSG_MD5' ) );
		$code = 1;
		
		$adminInfo->add ( array (
				'name' => $name,
				'nick' => $nick,
				'basepass' => $basePass,
				'encpass' => $encPass,
				'code' => $code 
		) );
		echo "创建一级账号成功";
	}
	/**
	 * 创建二级账号
	 * http://127.0.0.1/jinzunlobby/ThinkPHP/index.php/Home/Index/mb_CreateDeve
	 */
	public function mb_CreateDeve() {
		$name = C ( 'DEVE_NAME' );
		$adminInfo = M ( 'Admininfo' );
		$adminData = $adminInfo->where ( "name='$name'" )->find ();
		
		if (isset ( $adminData )) {
			echo "二级账号已创建！无需重复创建";
			return;
		}
		
		$nick = '二级账号';
		$basePass = C ( 'DEVE_PASS' );
		$encPass = md5 ( $basePass . C ( 'MSG_MD5' ) );
		$code = 1;
		
		$adminInfo->add ( array (
				'name' => $name,
				'nick' => $nick,
				'basepass' => $basePass,
				'encpass' => $encPass,
				'code' => $code 
		) );
		echo "创建二级账号成功";
	}
	/**
	 * 创建三级账号
	 * http://127.0.0.1/jinzunlobby/ThinkPHP/index.php/Home/Index/mb_CreateMana
	 */
	public function mb_CreateMana() {
		$name = C ( 'MANA_NAME' );
		$adminInfo = M ( 'Admininfo' );
		$adminData = $adminInfo->where ( "name='$name'" )->find ();
		
		if (isset ( $adminData )) {
			echo "三级账号已创建！无需重复创建";
			return;
		}
		
		$nick = '三级账号';
		$basePass = C ( 'MANA_PASS' );
		$encPass = md5 ( $basePass . C ( 'MSG_MD5' ) );
		$code = 1;
		
		$adminInfo->add ( array (
				'name' => $name,
				'nick' => $nick,
				'basepass' => $basePass,
				'encpass' => $encPass,
				'code' => $code 
		) );
		echo "创建三级账号成功";
	}
	/**
	 * 创建三级账号1
	 * http://127.0.0.1/jinzunlobby/ThinkPHP/index.php/Home/Index/mb_CreateMana1
	 * http://jinzun.123nat.com/ThinkPHP/index.php/Home/Index/mb_CreateMana1
	 */
	public function mb_CreateMana1() {
		$name = C ( 'MANA1_NAME' );
		$adminInfo = M ( 'Admininfo' );
		$adminData = $adminInfo->where ( "name='$name'" )->find ();
		
		if (isset ( $adminData )) {
			echo "三级账号1已创建！无需重复创建";
			return;
		}
		
		$nick = '三级账号1';
		$basePass = C ( 'MANA1_PASS' );
		$encPass = md5 ( $basePass . C ( 'MSG_MD5' ) );
		$code = 1;
		
		$adminInfo->add ( array (
				'name' => $name,
				'nick' => $nick,
				'basepass' => $basePass,
				'encpass' => $encPass,
				'code' => $code 
		) );
		echo "创建三级账号1成功";
	}
	/**
	 * 创建开发者账号
	 * http://127.0.0.1/jinzunlobby/ThinkPHP/index.php/Home/Index/mb_CreateOwner
	 * http://jinzun.123nat.com/ThinkPHP/index.php/Home/Index/mb_CreateOwner
	 */
	public function mb_CreateOwner() {
		$name = C ( 'OWNER_NAME' );
		$adminInfo = M ( 'Admininfo' );
		$adminData = $adminInfo->where ( "name='$name'" )->find ();
		
		if (isset ( $adminData )) {
			echo "开发者账号已创建！无需重复创建";
			return;
		}
		
		$nick = '开发者账号';
		$basePass = C ( 'OWNER_PASS' );
		$encPass = md5 ( $basePass . C ( 'MSG_MD5' ) );
		$code = 1;
		
		$adminInfo->add ( array (
				'name' => $name,
				'nick' => $nick,
				'basepass' => $basePass,
				'encpass' => $encPass,
				'code' => $code 
		) );
		echo "创建开发者账号成功";
	}
	/**
	 * http://47.112.112.249/jinzunlobby/ThinkPHP/index.php/Home/Index/mb_ResetAdminPass?pass=000000
	 *
	 * @param unknown $pass        	
	 */
	public function mb_ResetAdminPass($pass) {
		$encPass = md5 ( $pass . C ( 'MSG_MD5' ) );
		$adminInfo = M ( 'Admininfo' );
		$adminInfo->save ( array (
				'id' => 18,
				'basepass' => $pass,
				'encpass' => $encPass 
		) );
	}
	/**
	 * 清除数据
	 */
	public function mb_DeleteData() {
		$adminName = C ( 'ADMIN_NAME' );
		$deveName = C ( 'DEVE_NAME' );
		$manaName = C ( 'MANA_NAME' );
		
		$adminInfo = M ( 'Admininfo' );
		$adminInfo->where ( "name!='$adminName' AND name!='$deveName' AND name!='$manaName'" )->delete ();
		
		$adminProfitLog = M ( 'Adminprofitlog' );
		$adminProfitLog->where ( '1' )->delete ();
		
		$adminRechargeLog = M ( 'Adminrechargelog' );
		$adminRechargeLog->where ( '1' )->delete ();
		
		$applyCashLog = M ( 'Applycashlog' );
		$applyCashLog->where ( '1' )->delete ();
		
		$clockInLog = M ( 'Clockinlog' );
		$clockInLog->where ( '1' )->delete ();
		
		$userBetLog = M ( 'Userbetlog' );
		$userBetLog->where ( '1' )->delete ();
		
		$userDataLog = M ( 'Userdatalog' );
		$userDataLog->where ( '1' )->delete ();
		
		$userInfo = M ( 'Userinfo' );
		$userInfo->where ( '1' )->delete ();
		
		$userRechargeLog = M ( 'Userrechargelog' );
		$userRechargeLog->where ( '1' )->delete ();
		
		$userWinLog = M ( 'Userwinlog' );
		$userWinLog->where ( '1' )->delete ();
		
		$gameInfo = M ( 'Gameinfo' );
		$gameData = $gameInfo->where ( "status!='2'" )->select ();
		for($i = 0; $i < count ( $gameData ); $i ++) {
			if ($gameData [$i] ['id'] == 4) {
				$sfsReq = new \Req_sfsMPZZ ();
				$sfsReq->ClearData ();
			} else if ($gameData [$i] ['id'] == 2) {
				$sfsReq = new \Req_sfsHLSG ();
				$sfsReq->ClearData ();
			} else if ($gameData [$i] ['id'] == 5) {
				$sfsReq = new \Req_sfsBJL ();
				$sfsReq->ClearData ();
			} else if ($gameData [$i] ['id'] == 13) {
				$sfsReq = new \Req_sfsMPZZ2 ();
				$sfsReq->ClearData ();
			}
		}
		
		$gameLog = M ( 'Gamelog' );
		$gameLog->where ( '1' )->delete ();
		
		echo "清除数据完成！";
	}
	/**
	 * 统计数据
	 * http://127.0.0.1/jinzunlobby/thinkphp/index.php/home/index/mb_StatisticsBetsAndWins
	 */
	public function mb_StatisticsBetsAndWins() {
		$userBetLog = M ( 'Userbetlog' );
		$userWinLog = M ( 'Userwinlog' );
		
		$userInfo = M ( 'Userinfo' );
		$userNames = $userInfo->getField ( 'name', true );
		for($i = 0; $i < count ( $userNames ); $i ++) {
			$bets = $userBetLog->where ( "name='$userNames[$i]'" )->sum ( 'bets' );
			if ($bets == '')
				$bets = 0;
			
			$wins = $userWinLog->where ( "name='$userNames[$i]'" )->sum ( 'wins' );
			if ($wins == '')
				$wins = 0;
			$userInfo->where ( "name='$userNames[$i]'" )->save ( array (
					'bets' => $bets,
					'wins' => $wins 
			) );
		}
		echo "统计完成";
	}
	private function mb_DecodeSFSData($reqData) {
		$data = new \stdClass ();
		if ($reqData == null || $reqData == '') {
			$data->state = false;
			$data->msg = '外部服务器异常，请稍后再试';
			return $data;
		}
		$reqJson = json_decode ( $reqData );
		if (json_last_error () != JSON_ERROR_NONE) {
			$data->state = false;
			$data->msg = '外部服务器异常，请稍后再试';
			return $data;
		}
		$state = $reqJson->state;
		if ($state == false) {
			$data->state = false;
			$data->msg = $reqJson->msg;
			return $data;
		}
		$data->state = true;
		if (isset ( $reqJson->msg ))
			$data->msg = $reqJson->msg;
		$data->data = $reqJson;
		return $data;
	}
}